package com.hcyy.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcyy.entity.WxJsApiPayParam;
import com.hcyy.service.CustOrderService;
import com.hcyy.util.StreamUtil;
import com.hcyy.util.XmlUtil;
import com.hcyy.weixinUtil.ApiResult;
import com.hcyy.weixinUtil.SnsAccessToken;
import com.hcyy.weixinUtil.SnsAccessTokenApi;
import com.hcyy.weixinUtil.UserApi;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/WeiXin")
public class WeiXinController {
	@Autowired
	private CustOrderService custOrderService;
	@RequestMapping(value="/GetWxUser",method=RequestMethod.POST)
	@ResponseBody
	public String getWxUser(@RequestBody Map<String,Object> map){
		String code=(String) map.get("code");
		String appId="wx4696e4b4b8ae570d";
		String secret="961646f1a644f0ccc02565aef0c2d17f";
		SnsAccessToken token=SnsAccessTokenApi.getSnsAccessToken(appId, secret, code);
        String access_token=token.getAccessToken();
		String openId=token.getOpenid();
		ApiResult result=UserApi.getUserInfo(openId);
		String json="";
		/*String json=result.getJson();*/
		return json;
	}
	
	@RequestMapping(value="/GetOpenId",method=RequestMethod.POST)
	@ResponseBody
	public String getOpenId(@RequestBody Map<String,Object> map){
		String code=(String) map.get("code");
		String appId="wx4696e4b4b8ae570d";
		String secret="961646f1a644f0ccc02565aef0c2d17f";
		SnsAccessToken token=SnsAccessTokenApi.getSnsAccessToken(appId, secret, code);
		String openId=token.getOpenid();
		return openId;
	}
	
	@RequestMapping(value="/WxJsApiPay",method=RequestMethod.POST)
	@ResponseBody
	public String WxJsApiPay(@RequestBody Map<String,Object> map) throws UnknownHostException{
//		InetAddress ia=null;
		String platformcode=(String) map.get("platformcode");
		String openid=(String) map.get("openid");
		String sid=(String) map.get("sid");
		String totalfee=(String) map.get("totalfee");
		WxJsApiPayParam payParam=new WxJsApiPayParam();
		payParam.setOpenid(openid);
		//payParam.setPlatformcode(platformcode);
		payParam.setProductid(sid);
		payParam.setAttach("");
		payParam.setBody("product");
		payParam.setOut_trade_no(sid);
		payParam.setTotal_fee(totalfee);
		payParam.setTrade_type("JSAPI");
//		payParam.setSpbill_create_ip(ia.getHostAddress());
		payParam.setSpbill_create_ip(InetAddress.getLocalHost().getHostAddress());
		JSONObject json = JSONObject.fromObject(payParam);//将java对象转换为json对象
		String str = json.toString();//将json对象转换为字符串
		return str;
	}
	
	//支付成功，回调
	@RequestMapping(value="/PaySuccess",method=RequestMethod.POST)
	public void callBack(HttpServletRequest request) throws IOException{
		System.out.println("B2B微信支付成功");
		InputStream is = request.getInputStream();
		String xml = StreamUtil.inputStream2String(is, "UTF-8");
		//获取订单号
		String out_trade_no=XmlUtil.getOut_trade_no(xml);
		//根据订单号回写订单表，字段isPay=1,FTimePay=当前时间,订单log表记录日志
		custOrderService.paySuccess(out_trade_no);
	}
	
}
